package com.yao.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yao.common.annotation.User;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.sys.entity.dto.UserRoleRelDto;
import com.yao.sys.service.UserRoleRelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户角色关系管理
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "用户角色关系管理")
@RestController
@RequestMapping(value = "/userRoleRel")
@Slf4j
public class UserRoleRelController {

    @Autowired
    private UserRoleRelService userRoleRelService;

    @ApiOperation(value = "新增用户角色关系", notes = "新增用户角色关系")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody UserRoleRelDto model, @User Integer userId) {

        model.setOperator(userId);
        ValidateHelper.validateNull(model, new String[] {"userId", "roleIdList"});


        userRoleRelService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改用户角色关系", notes = "修改用户角色关系")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody UserRoleRelDto model, @User Integer userId) {

        model.setOperator(userId);
        ValidateHelper.validateNull(model, new String[] {"userId", "roleIdList"});

        userRoleRelService.modifyByUserId(model);

        return ApiResponse.success();

    }

    @ApiOperation(value = "删除用户角色关系", notes = "逻辑删除用户角色关系")
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteByUserId(@PathVariable("userId") Integer userId) {

        ValidateHelper.validateId(userId);

        userRoleRelService.removeByUserId(userId);

        return ApiResponse.success();

    }

}